const util = require('../../../../../utils/util.js');
const api = require('../../../../../config/api.js');
const app = getApp();
Page({
  data: {
    //顶部标签
    lables: [{ 'name': '圈子', 'img': 'https://image.xingqubaike.com:8443/minicard/ucenter/personhome/homesetup/04-00quanzi.png' }, { 'name': '宝贝', 'img': 'https://image.xingqubaike.com:8443/minicard/ucenter/personhome/homesetup/04-01baobei.png' }, { 'name': '店铺', 'img': 'https://image.xingqubaike.com:8443/minicard/ucenter/personhome/homesetup/04-02dianpu.png' }, { 'name': '专辑', 'img': 'https://image.xingqubaike.com:8443/minicard/ucenter/personhome/homesetup/04-03zhuanji.png' }, { 'name': '手帐', 'img': 'https://image.xingqubaike.com:8443/minicard/ucenter/personhome/homesetup/04-04shouzhang.png' }, { 'name': '星球', 'img': 'https://image.xingqubaike.com:8443/minicard/ucenter/personhome/homesetup/04-05xingqiu.png' }],
    //控制面板选择
    selectf: 0,
    //圈子页数
    quanPageNum: 1,
    //更多功能面板控制
    funShow: false,
    //临时储存数据
    info: {},
    //圈子数据
    quanziarr: [],
  },
  //加载用户
  onLoad: function (option) {
    var _this = this
    _this.setData({
      logininfo: app.globalData.logininfo,
      openid: app.globalData.logininfo.openid,
      getOpenid: option.openid,
    });
    if (_this.data.openid == _this.data.getOpenid) {
      wx.setNavigationBarTitle({
        title: "我的收藏"
      })
    } else {
      wx.setNavigationBarTitle({
        title: "Ta的收藏"
      })
    }
    var selectOpenid = _this.data.getOpenid
    var openid = app.globalData.logininfo.openid;
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.openid = openid;
        data.selectOpenid = selectOpenid
        data.access_token = res.access_token;
        data.pageNum = 1;
        data.pageSize = 20;
        util.post(api.Home_CollectionCircleApiUrl, data).then(function (res2) {
          if (res2.code == 200) {
            var resultList = res2.result.list;
            var arr = _this.data.quanziarr;
            _this.setData({
              quanziarr: resultList
            })
            console.log(_this.data.quanziarr)
          }
        })
      });
    }
  },
  //选择面板
  selectfunction: function (e) {
    var sIndex = e.currentTarget.dataset.index
    var _this = this
    console.log(sIndex)
    this.setData({
      selectf: sIndex
    })
    var selectOpenid = _this.data.getOpenid
    var openid = app.globalData.logininfo.openid;
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.openid = openid;
        data.selectOpenid = selectOpenid
        data.access_token = res.access_token;
        data.pageNum = 1;
        data.pageSize = 20;
        //获取收藏圈子
        if (sIndex == 0) {
          util.post(api.Home_CollectionCircleApiUrl, data).then(function (res2) {
            if (res2.code == 200) {
              var resultList = res2.result.list;
              var arr = _this.data.quanziarr;
              _this.setData({
                quanziarr: resultList,
                quanPageNum: 1
              })
            }
          })
        }
      });
    }
  },
  /**
   * 进入圈子详情页面
   */
  navigateToCircleInfo: function (e) {
    var _this = this;
    var circleid = e.currentTarget.dataset.circleid;
    var openid = app.globalData.logininfo.openid;
    var getopenid = _this.data.getopenid;
    var data = {};
    data.circleid = circleid;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        _this.userIsCircleJoinQuery(api.Home_UserIsCircleJoinApiUrl, data);
      })
    } else {
      _this.userIsCircleJoinQuery(api.Home_UserIsCircleJoinUrl, data);
    }
  },
  /**
   * 校验用户是否为已加入的圈子：请求
   */
  userIsCircleJoinQuery: function (url, data) {
    var _this = this;
    util.post(url, data).then(function (res) {
      var userIsCircleJoin = false;
      if (res.code == 200) {
        userIsCircleJoin = res.result.userIsCircleJoin;
      }
      if (userIsCircleJoin) {
        // 已加入
        wx.navigateTo({
          url: '../../personcenter/editdetails/circleinteraction/circleinteraction?circleid=' + data.circleid,
        })
      } else {
        // 未加入
        wx.navigateTo({
          url: '../../personcenter/editdetails/circledetails/circledetails?circleid=' + data.circleid,
        })
      }
    });
  },
  showPop: function(e){
    var _this = this
    var circleid = e.currentTarget.dataset.circleid;
    var index = e.currentTarget.dataset.index;
    var type = e.currentTarget.dataset.type;
    if(type=="quan"){
      var arr = _this.data.quanziarr
      if (arr[index].isPlacedtop==0){
        _this.setData({
          popText: "置顶"
        })
      }else{
        _this.setData({
          popText: "取消置顶"
        })
      }
    }
    var info ={}
    info.circleid = circleid
    info.index = index
    info.type = type
    _this.setData({
      funShow: true,
      info: info
    })
  },
  onClose: function(){
    var _this = this
    _this.setData({
      funShow: false
    })
  },
  //取消收藏操作
  delshoucang: function(){
    var _this = this
    var info = _this.data.info
    var openid = app.globalData.logininfo.openid;
    var data = {};
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        if (info.type == "quan") {
          data.id = info.circleid;
          data.type = 1;
          data.openid = openid;
          data.operation = 1;
          data.access_token = res.access_token;
          util.post(api.Home_ChoicenessCircleTapApiUrl, data).then(function (res2) {
            if (res2.code == 200) {
              var arr = _this.data.quanziarr
              arr.splice(info.index, 1)
              _this.setData({
                quanziarr: arr
              })
              _this.onClose();
            }
          })
        }
      })
    }
  },
  zhiding: function(){
    var _this = this
    var info = _this.data.info
    var openid = app.globalData.logininfo.openid;
    var data = {};
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        if (info.type == "quan") {
          data.id = info.circleid;
          data.openid = openid;
          data.access_token = res.access_token;
          util.post(api.CollectionCirclePlacedtop, data).then(function (res2) {
            if (res2.code == 200) {
              _this.onPullDownRefresh();
              _this.onClose();
            }
          })
        }
      })
    }
  },
  //拉伸顶部刷新
  onPullDownRefresh: function () {
    var _this = this
    var sId = _this.data.selectf
    var selectOpenid = _this.data.getOpenid
    var openid = _this.data.openid
    var data = {}
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.selectOpenid = selectOpenid
        data.access_token = res.access_token;
        data.pageNum = 1;
        data.pageSize = 20;
        if (sId == 0) {
          util.post(api.Home_CollectionCircleApiUrl, data).then(function (res) {
            if (res.code == 200) {
              var resultList = res.result.list;
              _this.setData({
                quanziarr: resultList,
                quanPageNum: 1
              })
            }
          });
        }
      });
    }
    wx.stopPullDownRefresh();
  },
  //拉伸底部加载
  onReachBottom: function () {
    var _this = this
    var sId = _this.data.selectf
    var selectOpenid = _this.data.getOpenid
    var openid = _this.data.openid
    var data = {}
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.selectOpenid = selectOpenid
        data.access_token = res.access_token;
        data.pageSize = 20;
        if (sId == 0) {
          var num = parseInt(_this.data.quanPageNum + 1);
          data.pageNum = num;
          util.post(api.Home_CollectionCircleApiUrl, data).then(function (res) {
            if (res.code == 200) {
              var detailsList = res.result.list;
              var arr = _this.data.quanziarr;
              for (var i = 0; i <= detailsList.length - 1; i++) {
                arr.push(detailsList[i])
              }
              _this.setData({
                quanziarr: arr,
                quanPageNum: num
              })
            }
          });
        }
      });
    }
    wx.stopPullDownRefresh();
  },
})